qcollect-traits 0.6.0

Traits for being generic over collection-types.
//! ### TODO 
//!     
//! * impl boxed
//! 

use range::*;
use traits::*;

use std::collections::Bound;

// ++++++++++++++++++++ ref ++++++++++++++++++++

impl<'r, T: ?Sized> OrderedCollection for &'r T
    where T: OrderedCollection
{}

impl<'r, T: ?Sized> Len for &'r T
    where T: Len
{
    fn len(&self) -> usize { (**self).len() }
    fn is_empty(&self) -> bool { (**self).is_empty() }
}

impl<'r, T: ?Sized> Capacity for &'r T
    where T: Capacity
{
    fn capacity(&self) -> usize { (**self).capacity() }
    fn free_space(&self) -> usize { (**self).free_space() }
}

impl<'r, T: ?Sized, Q> Contains<Q> for &'r T
    where T: Contains<Q>
{
    fn contains(&self, query: Q) -> bool { (**self).contains(query) }
}

impl<'r, 'a, T: ?Sized, Q> _Get<'a, Q> for &'r T
    where T: _Get<'a, Q>
{
    type Ret = T::Ret;
}

impl<'r, T: ?Sized + 'static, Q> Get<Q> for &'r T
    where T: Get<Q>
{
    fn get<'a>(&'a self, query: Q) -> <Self as _Get<'a, Q>>::Ret { (**self).get(query) }
}

impl<'r, 'a, T: ?Sized> _Iterate<'a> for &'r T
    where T: _Iterate<'a>
{
    type Iter = T::Iter;
}

impl<'r, T: ?Sized + 'static> Iterate for &'r T
    where T: Iterate
{
    fn iter<'a>(&'a self) -> <Self as _Iterate<'a>>::Iter { (**self).iter() }
}

// ++++++++++++++++++++ mut ref ++++++++++++++++++++

impl<'r, T: ?Sized> OrderedCollection for &'r mut T
    where T: OrderedCollection
{}

impl<'r, T: ?Sized> Len for &'r mut T
    where T: Len
{
    fn len(&self) -> usize { (**self).len() }
    fn is_empty(&self) -> bool { (**self).is_empty() }
}

impl<'r, T: ?Sized> Capacity for &'r mut T
    where T: Capacity
{
    fn capacity(&self) -> usize { (**self).capacity() }
    fn free_space(&self) -> usize { (**self).free_space() }
}

impl<'r, T: ?Sized> Reserve for &'r mut T
    where T: Reserve
{
    fn reserve(&mut self, n: usize){ (**self).reserve(n) }
    fn reserve_exact(&mut self, n: usize){ (**self).reserve_exact(n); }
}

impl<'r, T: ?Sized> Clear for &'r mut T
    where T: Clear
{
    fn clear(&mut self){ (**self).clear() }
}

impl<'r, T: ?Sized> ShrinkToFit for &'r mut T
    where T: ShrinkToFit
{
    fn shrink_to_fit(&mut self){ (**self).shrink_to_fit() }
}

impl<'r, T: ?Sized, Q> Contains<Q> for &'r mut T
    where T: Contains<Q>
{
    fn contains(&self, query: Q) -> bool { (**self).contains(query) }
}

impl<'r, 'a, T: ?Sized, Q> _Get<'a, Q> for &'r mut T
    where T: _Get<'a, Q>
{
    type Ret = T::Ret;
}

impl<'r, T: ?Sized + 'static, Q> Get<Q> for &'r mut T
    where T: Get<Q>
{
    fn get<'a>(&'a self, query: Q) -> <Self as _Get<'a, Q>>::Ret { (**self).get(query) }
}

impl<'r, 'a, T: ?Sized, Q> _GetMut<'a, Q> for &'r mut T
    where T: _GetMut<'a, Q>
{
    type Ret = T::Ret;
}

impl<'r, T: ?Sized + 'static, Q> GetMut<Q> for &'r mut T
    where T: GetMut<Q>
{
    fn get_mut<'a>(&'a mut self, query: Q) -> <Self as _GetMut<'a, Q>>::Ret { (**self).get_mut(query) }
}

impl<'r, 'a, T: ?Sized> _Iterate<'a> for &'r mut T
    where T: _Iterate<'a>
{
    type Iter = T::Iter;
}

impl<'r, T: ?Sized + 'static> Iterate for &'r mut T
    where T: Iterate
{
    fn iter<'a>(&'a self) -> <Self as _Iterate<'a>>::Iter { (**self).iter() }
}

impl<'r, 'a, T: ?Sized> _IterateMut<'a> for &'r mut T
    where T: _IterateMut<'a>
{
    type IterMut = T::IterMut;
}

impl<'r, T: ?Sized + 'static> IterateMut for &'r mut T
    where T: IterateMut
{
    fn iter_mut<'a>(&'a mut self) -> <Self as _IterateMut<'a>>::IterMut { (**self).iter_mut() }
}

impl<'r, T: ?Sized, Val> Insert<Val> for &'r mut T 
    where T: Insert<Val>
{
    type Ret = T::Ret;
    fn insert(&mut self, val: Val) -> Self::Ret { (**self).insert(val) }
}

impl<'r, T: ?Sized, Key, Val> Insert2<Key, Val> for &'r mut T 
    where T: Insert2<Key, Val>
{
    type Ret = T::Ret;
    fn insert(&mut self, key: Key, val: Val) -> Self::Ret { (**self).insert(key, val) }
}

impl<'r, T: ?Sized, Q> Remove<Q> for &'r mut T 
    where T: Remove<Q>
{
    type Ret = T::Ret;
    fn remove(&mut self, query: Q) -> Self::Ret { (**self).remove(query) }
}

impl<'r, T: ?Sized, Val> PushFront<Val> for &'r mut T 
    where T: PushFront<Val>
{
    fn push_front(&mut self, val: Val) { (**self).push_front(val) }
}

impl<'r, T: ?Sized, Val> PushBack<Val> for &'r mut T 
    where T: PushBack<Val>
{
    fn push_back(&mut self, val: Val) { (**self).push_back(val) }
}

impl<'r, T: ?Sized> PopFront for &'r mut T 
    where T: PopFront
{
    type Val = T::Val;
    fn pop_front(&mut self) -> Option<Self::Val> { (**self).pop_front() }
}

impl<'r, T: ?Sized> PopBack for &'r mut T 
    where T: PopBack
{
    type Val = T::Val;
    fn pop_back(&mut self) -> Option<Self::Val> { (**self).pop_back() }
}

// ++++++++++++++++++++ slice ++++++++++++++++++++

use std::slice;

impl<T> Len for [T] {
    fn len(&self) -> usize { (*self).len() }
} 

/// TODO: add `Q: PartialEq<T>`-parameter?
impl<'i, T: PartialEq> Contains<&'i T> for [T] {
    fn contains(&self, elt: &'i T) -> bool { (*self).contains(elt) }
}

impl<'a, T: 'a> _Get<'a, usize> for [T] {
    type Ret = Option<&'a T>;
}

impl<T: 'static> Get<usize> for [T] {
    fn get<'a>(&'a self, idx: usize) -> <Self as _Get<'a, usize>>::Ret { (*self).get(idx) }
}

impl<'a, T: 'a> _GetMut<'a, usize> for [T] {
    type Ret = Option<&'a mut T>;
}

impl<T: 'static> GetMut<usize> for [T] {
    fn get_mut<'a>(&'a mut self, idx: usize) -> <Self as _GetMut<'a, usize>>::Ret { (*self).get_mut(idx) } 
}

impl<'a, T: 'a, R> _Get<'a, R> for [T] 
    where R: RangeArgument<usize>
{
    type Ret = &'a [T];
}

impl<T: 'static, R> Get<R> for [T] 
    where R: RangeArgument<usize>
{
    fn get<'a>(&'a self, range: R) -> <Self as _Get<'a, R>>::Ret {
        let (start, end) = range.into_bounds();

        let start_idx = match start {
            Bound::Included(x) => x,
            Bound::Excluded(x) => x + 1,
            Bound::Unbounded => 0,
        };
        let end_idx = match end {
            Bound::Included(x) => x + 1,
            Bound::Excluded(x) => x,
            Bound::Unbounded => self.len(),
        };

        &self[start_idx..end_idx]
    }
}

impl<'a, T: 'a, R> _GetMut<'a, R> for [T] 
    where R: RangeArgument<usize>
{
    type Ret = &'a mut [T];
}

impl<T: 'static, R> GetMut<R> for [T] 
    where R: RangeArgument<usize>
{
    fn get_mut<'a>(&'a mut self, range: R) -> <Self as _GetMut<'a, R>>::Ret {
        let (start, end) = range.into_bounds();

        let start_idx = match start {
            Bound::Included(x) => x,
            Bound::Excluded(x) => x + 1,
            Bound::Unbounded => 0,
        };
        let end_idx = match end {
            Bound::Included(x) => x + 1,
            Bound::Excluded(x) => x,
            Bound::Unbounded => self.len(),
        };

        &mut self[start_idx..end_idx]
    }
}

impl<'a, T: 'a> _Iterate<'a> for [T] {
    type Iter = slice::Iter<'a, T>;
}

impl<T: 'static> Iterate for [T] {
    fn iter<'a>(&'a self) -> <Self as _Iterate<'a>>::Iter { (*self).iter() }
}

impl<'a, T: 'a> _IterateMut<'a> for [T] {
    type IterMut = slice::IterMut<'a, T>;
}

impl<T: 'static> IterateMut for [T] {
    fn iter_mut<'a>(&'a mut self) -> <Self as _IterateMut<'a>>::IterMut { (*self).iter_mut() }
}

#[test]
fn test_get_range() {
    let c: &[_] = &[0, 10, 20, 30, 40, 50];

    assert_eq!(&c[..], (&c).get(..)); 
    assert_eq!(&c[2..], (&c).get(2..)); 
    assert_eq!(&c[2..4], (&c).get(2..4)); 
    assert_eq!(&c[..4], (&c).get(..4));

    assert_eq!(&c[2..], (&c).get((Bound::Excluded(1), Bound::Unbounded))); 
    assert_eq!(&c[..4], (&c).get((Bound::Unbounded, Bound::Included(3)))); 
}